package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.common.BaseContext;
import com.example.demo.common.Result;
import com.example.demo.entity.Bill;
import com.example.demo.service.AccountService;
import com.example.demo.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private BillService billService;

    /**
     * 查询用户钱包余额
     * @return
     */
    @PostMapping("get")
    public Result<BigDecimal> getBalance(){
        return Result.success(accountService.getBalance());
    }

    /**
     * 用户消费100元
     * @param password
     */
    @PostMapping("pay")
    public Result<String> pay(@RequestBody String password, Long orderId){
        accountService.pay(password, orderId);
        return Result.success("支付成功");
    }

    /**
     * 用户退款20元
     * @param orderId
     */
    @PostMapping("refund")
    public Result<String> refund(Long orderId){
        accountService.refund(orderId);
        return Result.success("退款成功");
    }

    /**
     * 查询用户钱包金额变动明细
     * @return
     */
    @GetMapping("list")
    public Result<List<Bill>> list(){
        BaseContext.setCurrentId(1L);
        LambdaQueryWrapper<Bill> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bill::getAccountId, BaseContext.getCurrentId());
        List<Bill> list = billService.list(wrapper);
        return Result.success(list);
    }
}
